import Pano from 'Comps/Pano'
import Loading from 'Comps/Loading'
import AerialGroup from 'Comps/AerialGroup'
import Introduction from 'Comps/AerialGroup/Introduction'

// iframe
import StartRemindIframe from 'Iframe/startRemind'

import { getViewport, getPlatform, getIP, isWxBrowser } from '@/utils'
import { initBgMusic, updateBgMusicStatus } from '@/utils/audio'
import { mapGetters } from 'vuex'

export default {
  components: {
    StartRemindIframe,
    Pano,
    Loading,
    AerialGroup,
    Introduction,
  },
  data() {
    return {
      isShowLoading: false,
    }
  },
  computed: {
    ...mapGetters([
      'themeId',
      'loadInfo',
      'startPicOrVideo',
      'aerialData',
      'hotspots',
    ]),
    getHasStartRemind() {
      return this.startPicOrVideo
    },
    getHasLoader() {
      return this.loadInfo
    },
    getTitle() {
      if (this.title) {
        return this.title
      }
      return ''
    },
    getThemeType() {
      return this.themeId
    },
    getStartRemind() {
      return this.startPicOrVideo ? true : false
    },
    getSceneList() {
      if (this.aerialData && this.aerialData.group_scene_list && this.aerialData.group_scene_list.length > 0) {
        let filterArr = arr.filter(item => (item.scenelist && item.scenelist.length > 0))
        console.log(filterArr)
        return filterArr
      } else {
        return []
      }
    }
  },
  watch: {
    aerialData: {
      handler(data) {
        if (data) {
          if (data.isloading == 1) {
            this.isShowLoading = true
          }
          document.title = data.pano_name || '全景'
          this.title = data.pano_name
        }
      },
      immediate: true,
    },
  },
  mounted() {
    // 监听是否开场提示
    const hasStartPic = () => {
      const re = /mp4|AVI|wma|rmvb|mid|3GP/i
      // 开场不是视频直接播放音乐initBgMusic
      if (!(this.getHasStartRemind && re.test(this.startPicOrVideo))) {
        if (!this.hasVideoInHotspot() || isIos()) {
          initBgMusic()
        } else {
          updateBgMusicStatus()
        }
      }
    }

    // 如果不是微信浏览器，判断是否有Loader
    if (!isWxBrowser()) {
      if (!this.getHasLoader) {
        setTimeout(() => {
          hasStartPic()
        }, 3000)
      } else {
        setTimeout(() => {
          hasStartPic()
        }, this.getHasLoader.showtime + 3000)
      }
    } else {
      // 初始化音频
      document.addEventListener('WeixinJSBridgeReady', function() {
        hasStartPic()
      }, false)
    }

    // 发送用户数据
    setTimeout(() => {
      //实例化城市查询类
      var cityData
      var userData = {
        sw: getViewport().VW,
        sh: getViewport().VH,
        ip: getIP(),
        city: '',
        region: '',
        type: getPlatform() ? 1 : 2,
        panoid: this.$store.getters.panoId,
        userid: this.$store.getters.aerialData
          ? this.$store.getters.aerialData.user_id
          : '',
      }

      try {
        let citysearch = new AMap.CitySearch()

        //自动获取用户IP，返回当前城市
        citysearch.getLocalCity((status, result) => {
          if (status === 'complete' && result.info === 'OK') {
            cityData = result
            userData.city = cityData ? cityData.city : ''
            userData.region = cityData ? cityData.province : ''
            this.$store.dispatch('sendCustomInfo', userData)
          }
        })
      } catch (e) {
        this.$store.dispatch('sendCustomInfo', userData)
      }
    }, 5000)
  },
  methods: {
    hasVideoInHotspot () {
      let isInit = false
      const id = $VM.$krp.getSceneId()
      if (this.hotspots && this.hotspots[id] && this.hotspots[id].hotspotlist && this.hotspots[id].hotspotlist.length > 0) {
        const hotspotlist = this.hotspots[id].hotspotlist
        hotspotlist.map(item => {
          // 处理音乐重叠
          if (item.diy_id == 0 && item.type_id == 9) {
            isInit = true
          }
        })
      }
      return isInit
    },
    closeLoading() {
      this.isShowLoading = false
    },
  },
}
